]>
Commit | Line | Data |
---|---|---|
5802c153 RBR |
1 | /* |
2 | Copyright (C) 2024 Rubén Beltrán del Río | |
3 | ||
4 | This program is free software: you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation, either version 3 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program. If not, see https://captura.tranquil.systems. | |
16 | */ | |
f5d16c1c RBR |
17 | import CoreGraphics |
18 | ||
19 | extension CGImage { | |
20 | func resize(by scale: CGFloat) -> CGImage? { | |
21 | let width = Int(CGFloat(self.width) / scale) | |
22 | let height = Int(CGFloat(self.height) / scale) | |
505c1e62 | 23 | |
f5d16c1c RBR |
24 | let bitsPerComponent = self.bitsPerComponent |
25 | let colorSpace = self.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)! | |
26 | let bitmapInfo = self.bitmapInfo.rawValue | |
505c1e62 RBR |
27 | |
28 | guard | |
29 | let context = CGContext( | |
30 | data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: 0, | |
31 | space: colorSpace, bitmapInfo: bitmapInfo) | |
32 | else { | |
33 | return nil | |
f5d16c1c | 34 | } |
505c1e62 | 35 | |
f5d16c1c RBR |
36 | context.interpolationQuality = .high |
37 | context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height)) | |
505c1e62 | 38 | |
f5d16c1c RBR |
39 | return context.makeImage() |
40 | } | |
41 | } |